var rkpz = {
    setBtnStatus:function () {
        var row = $('#grid_rkpz').datagrid('getSelected');
        if (row && row.sfqydhdrk == '是') {
            $("#btn_rkpz_kq").hide();
            $("#btn_rkpz_gb").show();
        } else if (row && row.sfqydhdrk =='否' ) {
            $("#btn_rkpz_kq").show();
            $("#btn_rkpz_gb").hide();
        }
    }
}

$(function () {
    $("#rkpz_name").gsjgtree(1);

    //表格初始加载
    $('#grid_rkpz').datagrid({
        url:$.webPath() +"/kcgl/rkpz/rkpzList",
        toolbar:"#toolbar_rkpz",
        pagination:true,
        rownumbers:true,
        fitColumns:true,
        singleSelect:true,
        pageNumber: 1,
        fit:true,
        striped:true,
        columns:[[
            {field:'jc', title:'公司名称',align: 'center', width:100},
            {field:'sfqydhdrk', title:'是否启用到货单开关状态', width:200,align: 'center',
                formatter: function (value) {
                    if (value == '是')
                        return '开启';
                    if (value == '否')
                        return '关闭';
                }}
        ]],
        onLoadSuccess:rkpz.setBtnStatus,
        onClickRow:rkpz.setBtnStatus
    });
    //**********按钮触发事件**********
    //查询按钮
    $("#btn_rkpz_search").click(function () {
        rkpz_search();
    });
// 开启按钮
    $("#btn_rkpz_kq").click(function () {
        rkpz_kq();
    });
// 关闭按钮
    $("#btn_rkpz_gb").click(function () {
        rkpz_gb();
    });
});
//开启
function rkpz_kq() {
    var row = $('#grid_rkpz').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否开启此公司启用到货单功能？', function (r) {
            if (r) {
                $.ajax({
                    url:$.webPath() + "/kcgl/rkpz/kq",
                    dataType:'json',
                    type:'POST',
                    data:{gsjgId:row.id, sfqydhdrk:true},
                    beforeSend:function () {
                        $("#layout_rkpz").mask();
                    },
                    success:function (data) {
                        $("#layout_rkpz").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_rkpz').datagrid('reload');
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else {
                            $('#grid_rkpz').datagrid('reload');
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择公司！", "warning");
    }
}

//关闭
function rkpz_gb() {
    var row = $('#grid_rkpz').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否关闭此公司启用到货单功能？', function (r) {
            if (r) {
                $.ajax({
                    url:$.webPath() + "/kcgl/rkpz/gb",
                    dataType:'json',
                    type:'POST',
                    data:{gsjgId:row.id, sfqydhdrk:false},
                    beforeSend:function () {
                        $("#layout_rkpz").mask();
                    },
                    success:function (data) {
                        $("#layout_rkpz").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_rkpz').datagrid('reload');
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else {
                            $('#grid_rkpz').datagrid('reload');
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择公司！", "warning");
    }
}

//查询
function rkpz_search() {
    var rkpzName = $('#rkpz_name').combotree('getText') == null ? null : $('#rkpz_name').combotree('getText');

    $('#grid_rkpz').datagrid('load',
        {
            name:rkpzName
        }
    );
}
